package com.woniuxy.reader.controller;


import com.woniuxy.reader.entity.Author;
import com.woniuxy.reader.entity.Book;
import com.woniuxy.reader.entity.BookData;
import com.woniuxy.reader.service.AuthorService;
import com.woniuxy.reader.service.BookDataService;
import com.woniuxy.reader.service.BookService;
import com.woniuxy.result.ResponseResult;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zh_o
 * @since 2020-10-12
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private BookService bookService;

    @Resource
    private AuthorService authorService;

    @Resource
    private BookDataService bookDataService;

    /**
     　* @Author daxigua
     * @Description 传入书名，进行模糊查询，查找所有包含改字符串的书
     * @Date 2020/10/16  14:55
     * @return
     */
    @GetMapping("{name}")
    public ResponseResult selectBookByName(@PathVariable("name") String name){
        HashMap<Object, Object> map = new HashMap<>();
        List<Book> books = bookService.selectBookLikeBookName(name);
        List<Author> authors = authorService.selectAuthorLikeName(name);
        map.put("books",books);
        map.put("authors",authors);
        System.out.println(authors);
        return ResponseResult.success(map);
    }

    /**
     * 根据作者id返回该作者写的书籍集合
     */
    @GetMapping("/authorBook/{authorId}")
    public Object getBook(@PathVariable("authorId") Integer authorId){
        return ResponseResult.success(bookService.getBookByAid(authorId));
    }

    @ResponseBody
    @RequestMapping("/picture/add")
    public Object addlunbo(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        Assert.notNull(file, "上传文件不能为空");
        System.out.println(file);
//        String filepath = request.getServletContext().getRealPath("/lunbotu");
        String filepath ="D:\\hbhome\\woniu-reader-page.git\\public\\img";
        System.out.println(filepath);
        String filename = System.currentTimeMillis() + file.getOriginalFilename();
        System.out.println(filename);
        //确保路径存在
        File file2 = new File(filepath);
        if (!file2.exists()) {
            file2.mkdirs();
        }
        String savepath = filepath + "\\" + filename;
        System.out.println("作品图片保存路径:" + savepath);
        Map map = new HashMap<String, Object>();
        try {
            //保存文件到服务器
            file.transferTo(new File(savepath));
            //保存到数据库
            String mysqlpath = "/img/"+filename;
            //返回json
            map.put("msg", mysqlpath);
            map.put("code", 200);


        } catch (Exception e) {
            map.put("msg", "error");
            map.put("code", 0);
            e.printStackTrace();
        }

        return ResponseResult.success(map);
    }



    /**
     * 根据书籍id获取详情信息
     * @param bookId
     * @return
     */
    @GetMapping("/detail/{bookId}")
    public Object initBookDetail(@PathVariable("bookId") Integer bookId) {
        return ResponseResult.success(bookService.getBookDetailById(bookId));
    }
    /**
     　* @Author daxigua
     * @Description  传入book实体，进行书籍新增
     * @Date 2020/10/17  12:11
     * @Param  * @param null
     * @return
     */
    @PostMapping
    public ResponseResult insertbookByBook(Book book){
        int i = bookService.insertBookByBook(book);
        Integer bookId = book.getBookId();

        BookData bookData = new BookData();
        bookData.setBookId(bookId);
        bookData.setClickNumber(0);
        bookData.setCollection(0);
        bookData.setSubscribe(0);

        bookDataService.insertBookDataByBookData(bookData);

        System.out.println(bookId);

        if (bookId==0){
            return ResponseResult.error();
        }
        return ResponseResult.success(bookId);
    }
}

